﻿using DLL.NET50.DB.PIVAS.Entitys.DTO;
using DLL.NET60.DB.MS.Entity.DTO;
using DLL.Standard.Infrastructure;
using Mysqlx.Datatypes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace DLL.NET50.DB.MS.AdviceAndDrug
{
    [Serializable]
    public class CMS_AdviceAndDrug : CNotifyPropertyChanged, ICloneable
    {
        private DTO_adi_advice_execute _pivasAdvicePatient = new DTO_adi_advice_execute();
        private DTO_adi_advice_drug _adviceSolvent = new DTO_adi_advice_drug();
        private List<DTO_adi_advice_drug> _adviceDrug = new List<DTO_adi_advice_drug>();
        private List<DTO_adi_advice_drug> _pivasAdviceDrugs = new List<DTO_adi_advice_drug>();
        private List<DTO_adi_dic_drug> _Drugs = new List<DTO_adi_dic_drug>();
        private List<int> _DrugCountOfOneType = new List<int>();
        private DTO_adi_dic_drug _Solvent = new DTO_adi_dic_drug();

        private string _Info = "";
        private string _DrugCode = "";
        private int _enable = 0;//启用
        private int _reValue = 0;
        private int _AdviceHadStop = -1;//用来标记是否停嘱
        private int _AdviceID = 0;//用来标记医嘱序号
        private int _StopAdviceID = 0;//用来标记停嘱ID
        private int _SurplusMode = -1;//是来标记是否是结余模式
        private DateTime _ScanBarTime = DateTime.Now;

        public DTO_adi_advice_execute pivasAdvicePatient
        {
            get
            { return _pivasAdvicePatient; }
            set
            {
                _pivasAdvicePatient = value;
                OnPropertyChanged();
            }
        }

        public DTO_adi_advice_drug adviceSolvent
        {
            get
            { return _adviceSolvent; }
            set
            {
                _adviceSolvent = value;
                OnPropertyChanged();
            }
        }

        public List<DTO_adi_advice_drug> adviceDrug
        {
            get
            { return _adviceDrug; }
            set
            {
                _adviceDrug = value;
                OnPropertyChanged();
            }
        }

        public List<DTO_adi_advice_drug> pivasAdviceDrugs
        {
            get
            { return _pivasAdviceDrugs; }
            set
            {
                _pivasAdviceDrugs = value;
                OnPropertyChanged();
            }
        }

        public List<DTO_adi_dic_drug> Drugs
        {
            get
            { return _Drugs; }
            set
            {
                _Drugs = value;
                OnPropertyChanged();
            }
        }

        public DTO_adi_dic_drug Solvent
        {
            get
            { return _Solvent; }
            set
            {
                _Solvent = value;
                OnPropertyChanged();
            }
        }

        public List<int> DrugCountOfOneType
        {
            get
            { return _DrugCountOfOneType; }
            set
            {
                _DrugCountOfOneType = value;
                OnPropertyChanged();
            }
        }

        public string Info
        {
            get
            { return _Info; }
            set
            {
                _Info = value;
                OnPropertyChanged();
            }
        }

        public string DrugCode
        {
            get
            { return _DrugCode; }
            set
            {
                _DrugCode = value;
                OnPropertyChanged();
            }
        }

        public int Enable
        {
            get
            { return _enable; }
            set
            {
                _enable = value;
                OnPropertyChanged();
            }
        }

        public int reValue
        {
            get
            { return _reValue; }
            set
            {
                _reValue = value;
                OnPropertyChanged();
            }
        }

        public int AdviceHadStop //用来标记是否停嘱
        {
            get
            { return _AdviceHadStop; }
            set
            {
                _AdviceHadStop = value;
                OnPropertyChanged();
            }
        }

        public int AdviceID //用来标记医嘱序号
        {
            get
            { return _AdviceID; }
            set
            {
                _AdviceID = value;
                OnPropertyChanged();
            }
        }

        public int StopAdviceID //用来标记停嘱ID
        {
            get
            { return _StopAdviceID; }
            set
            {
                _StopAdviceID = value;
                OnPropertyChanged();
            }
        }

        public int SurPlusMode //
        {
            get
            { return _SurplusMode; }
            set
            {
                _SurplusMode = value;
                OnPropertyChanged();
            }
        }

        //ScanBarTime
        public DateTime ScanBarTime //
        {
            get
            { return _ScanBarTime; }
            set
            {
                _ScanBarTime = value;
                OnPropertyChanged();
            }
        }

        public int GetDrugCount()
        {
            int quantity = 0;
            for (int i = 0; i < adviceDrug.Count; i++)
            {
                quantity = quantity + Convert.ToInt32(adviceDrug[i].QUANTITY);
            }
            return quantity;
        }

        public float GetRealDose()
        {
            float allRealDose = 0;
            if (adviceDrug.Count > 1)
            {
                for (int i = 0; i < adviceDrug.Count; i++)
                {
                    float realDose = 0;
                    if ((adviceDrug[i].DOSE_UNIT == "mg") || (adviceDrug[i].DOSE_UNIT == "MG"))
                    {
                        realDose = adviceDrug[i].REALDOSE;
                    }
                    else if ((adviceDrug[i].DOSE_UNIT == "g") || (adviceDrug[i].DOSE_UNIT == "G"))
                    {
                        realDose = adviceDrug[i].REALDOSE * 1000.0f;
                    }
                    else
                    {
                        //可能是水针，单位是ml
                        realDose = adviceDrug[i].REALDOSE;
                    }
                    allRealDose = allRealDose + realDose;
                }
            }
            else
            {
                allRealDose = adviceDrug[0].REALDOSE;
            }
            return allRealDose;
        }

        public double GetRealQuatity()
        {
            double allRealQuantity = 0;
            if (adviceDrug.Count > 1)
            {
                for (int i = 0; i < adviceDrug.Count; i++)
                {
                    double realQuatity = 0;
                    realQuatity = adviceDrug[i].REALDOSE / adviceDrug[i].DOSE;
                    allRealQuantity = allRealQuantity + realQuatity;
                }
                double d = allRealQuantity;
                string str = d.ToString("N4"); //先转成字符串（保留4位小数，实际看具体精度要求而定）
                double.TryParse(str, out d);

                allRealQuantity = d;
            }
            else
            {
                double c = (adviceDrug[0].REALDOSE / adviceDrug[0].DOSE);
                double d = 0;
                string str = c.ToString("N4"); //先转成字符串（保留4位小数，实际看具体精度要求而定）
                double.TryParse(str, out d);

                allRealQuantity = d;
            }
            return allRealQuantity;
        }

        public float GetRealQuatity1()
        {
            float allRealQuantity = 0;
            if (adviceDrug.Count > 1)
            {
                for (int i = 0; i < adviceDrug.Count; i++)
                {
                    float realQuatity = 0;
                    realQuatity = Convert.ToSingle(adviceDrug[i].REAL_QUANTITY);
                    allRealQuantity = allRealQuantity + realQuatity;
                }
            }
            else
            {
                allRealQuantity = Convert.ToSingle(adviceDrug[0].REAL_QUANTITY);
            }
            return allRealQuantity;
        }

        public float GetDose()
        {
            float allDose = 0;

            if (adviceDrug.Count > 1)
            {
                for (int i = 0; i < adviceDrug.Count; i++)
                {
                    float Dose = 0;
                    if ((adviceDrug[i].DOSE_UNIT == "mg") || (adviceDrug[i].DOSE_UNIT == "MG"))
                    {
                        Dose = (adviceDrug[i].DOSE) * Convert.ToInt32(adviceDrug[i].QUANTITY);
                    }
                    else if ((adviceDrug[i].DOSE_UNIT == "g") || (adviceDrug[i].DOSE_UNIT == "G"))
                    {
                        Dose = adviceDrug[i].DOSE * 1000.0f * Convert.ToInt32(adviceDrug[i].QUANTITY);
                    }
                    else
                    {
                        //可能是水针，单位是ml
                        Dose = adviceDrug[i].DOSE * Convert.ToInt32(adviceDrug[i].QUANTITY);
                    }
                    allDose = allDose + Dose;
                }
            }
            else
            {
                allDose = adviceDrug[0].DOSE * Convert.ToInt32(adviceDrug[0].QUANTITY);
            }
            return allDose;
        }

        public string GetDoseUnit()
        {
            return Drugs[0].DOSE_UNIT;
        }

        public string GetRealDosUinit()
        {
            return adviceDrug[0].DOSE_UNIT;
        }

        public float GetFZZInVolume()
        {
            //非整只模式
            float allInVolume = Drugs[0].flush_Groups[0].flush_Params[0].IN_VOLUME;//注入西林瓶内的容积
            float dose = (adviceDrug[0].DOSE);
            float fzz_realdose = (adviceDrug[0].REALDOSE - ((Convert.ToUInt32(adviceDrug[0].QUANTITY) - 1) * adviceDrug[0].DOSE));//不是整只的实际剂量
            if (Drugs[0].SOLVENT_FLAG == 0)
            {
                //水针：普通溶媒
                if ((Drugs[0].NEEDLE_TYPE == 1))
                {
                    //水针,单位转换成毫升
                    allInVolume = Drugs[0].BOTTLE_VOLUME;
                }
            }
            else
            {
                //专用溶媒配药
                if (Drugs[0].SOLVENT_FLAG == 2)
                {
                    //专用溶媒：粉针
                    if (Convert.ToInt32(Drugs[0].NEEDLE_TYPE) == 1)
                    {
                        //水针
                        allInVolume = Drugs[0].flush_Groups[0].flush_Params[0].IN_VOLUME + Drugs[0].BOTTLE_VOLUME;//注入西林瓶内的容积
                    }
                }
            }
            return allInVolume;
        }

        public float GetFzzUsedDose()
        {
            float fzzUsedDose = 0;
            if (adviceDrug.Count > 0)
            {
                fzzUsedDose = (adviceDrug[0].REALDOSE - (Convert.ToUInt32((adviceDrug[0].QUANTITY) - 1) * adviceDrug[0].DOSE));//不是整只的实际剂量
            }
            return fzzUsedDose;
        }

        public float GetFZZOutVolume()
        {
            float fzz_OutVolume = 0;
            //非整只模式
            float allInVolume = Drugs[0].flush_Groups[0].flush_Params[0].IN_VOLUME;//注入西林瓶内的容积
            float dose = (adviceDrug[0].DOSE);
            float fzz_realdose = (adviceDrug[0].REALDOSE - (Convert.ToUInt32((adviceDrug[0].QUANTITY) - 1) * adviceDrug[0].DOSE));//不是整只的实际剂量
            if (Drugs[0].SOLVENT_FLAG == 0)
            {
                //水针：普通溶媒
                if ((Drugs[0].NEEDLE_TYPE == 1))
                {
                    //水针,单位转换成毫升
                    fzz_OutVolume = (Drugs[0].BOTTLE_VOLUME * (fzz_realdose / dose));//计算实际要抽出的荣没量
                }
                else
                {
                    //粉针： 普通溶媒模式,
                    fzz_OutVolume = (allInVolume * (fzz_realdose / dose));//计算实际要抽出的荣没量
                }
            }
            else
            {
                //专用溶媒配药
                if (Drugs[0].SOLVENT_FLAG == 2)
                {
                    //专用溶媒：粉针
                    if (Convert.ToInt32(Drugs[0].NEEDLE_TYPE) == 0)
                    {
                        //粉针
                        fzz_OutVolume = (allInVolume * (fzz_realdose / dose));//计算实际要抽出的荣没量
                    }
                    else
                    {
                        //水针
                        float allInVolume1 = Drugs[0].flush_Groups[0].flush_Params[0].IN_VOLUME + Drugs[0].BOTTLE_VOLUME;//注入西林瓶内的容积
                        fzz_OutVolume = (allInVolume1 * (fzz_realdose / dose));//计算实际要抽出的荣没量
                    }
                }
            }
            return fzz_OutVolume;
        }

        public object Clone()
        {
            CMS_AdviceAndDrug ad = new CMS_AdviceAndDrug();
            ad.Info = this.Info;
            ad.reValue = this.reValue;
            ad.Enable = this.Enable;
            ad.AdviceHadStop = this.AdviceHadStop;
            ad.AdviceID = this.AdviceID;
            ad.StopAdviceID = this.StopAdviceID;
            ad.SurPlusMode = this.SurPlusMode;
            ad.ScanBarTime = this.ScanBarTime;
            ad.adviceSolvent = (DTO_adi_advice_drug)this.adviceSolvent.Clone();
            ad.pivasAdvicePatient = (DTO_adi_advice_execute)this.pivasAdvicePatient.Clone();
            ad.Solvent = (DTO_adi_dic_drug)this.Solvent.Clone();

            for (int i = 0; i < this.adviceDrug.Count; i++)
            {
                ad.adviceDrug.Add((DTO_adi_advice_drug)this.adviceDrug[i].Clone());
            }
            for (int i = 0; i < this.Drugs.Count; i++)
            {
                ad.Drugs.Add((DTO_adi_dic_drug)this.Drugs[i].Clone());
            }
            for (int i = 0; i < this.pivasAdviceDrugs.Count; i++)
            {
                ad.pivasAdviceDrugs.Add((DTO_adi_advice_drug)this.pivasAdviceDrugs[i].Clone());
            }
            for (int i = 0; i < DrugCountOfOneType.Count; i++)
            {
                ad.DrugCountOfOneType.Add(this.DrugCountOfOneType[i]);
            }
            return ad;
        }
    }

    [Serializable]
    public class CMS_AdviceAndDrugs : ICloneable
    {
        private List<CMS_AdviceAndDrug> _adviceAndDrugs = new List<CMS_AdviceAndDrug>();

        public List<CMS_AdviceAndDrug> adviceAndDrugs
        {
            get
            { return _adviceAndDrugs; }
            set
            {
                _adviceAndDrugs = value;
                //OnPropertyChanged();
            }
        }

        private int _EK_Mode = 0;//只有单机模式有

        public int EK_Mode
        {
            get
            { return _EK_Mode; }
            set
            {
                _EK_Mode = value;
                //OnPropertyChanged();
            }
        }

        public List<string> GetScanBar()
        {
            List<string> scanBar = new List<string>();
            for (int i = 0; i < _adviceAndDrugs.Count; i++)
            {
                if (_adviceAndDrugs[i].Enable == 1)
                {
                    scanBar.Add(_adviceAndDrugs[i].pivasAdvicePatient.ADVICE_GROUP_NO);
                }
            }
            return scanBar;
        }

        public int GetDJClosePosIndex()
        {
            int closePosIndex = 0;
            for (int i = 0; i < _adviceAndDrugs.Count; i++)
            {
                if (_adviceAndDrugs[i].Enable == 1)
                {
                    if (_adviceAndDrugs[i].Drugs[0].BOTTLE_SIZE > closePosIndex)
                    {
                        closePosIndex = (int)_adviceAndDrugs[i].Drugs[0].BOTTLE_SIZE;
                    }
                }
            }
            return closePosIndex;
        }

        public object Clone()
        {
            //浅复制，只可以复制值类型的变量，对医用类型的变量没用
            CMS_AdviceAndDrugs mad = new CMS_AdviceAndDrugs();
            mad.EK_Mode = this.EK_Mode;
            //mad.adviceAndDrugs=CDeepCopy.CloneReflection(this.adviceAndDrugs);
            for (int i = 0; i < adviceAndDrugs.Count; i++)
            {
                mad.adviceAndDrugs.Add((CMS_AdviceAndDrug)this.adviceAndDrugs[i].Clone());
            }
            return mad;
        }
    }
}